<template>
  <div class="app-container">
    <el-form ref="financialInstruments" :model="financialInstruments" :rules="financialInstrumentsRules" label-width="80px" >
      <el-form-item label="债券代码" prop="debentureCode">
        <el-col :span="11">
          <el-input v-model="financialInstruments.debentureCode" placeholder="债券代码" style="width: 80%;"/>
        </el-col>
        <el-col :span="11">
          <el-form-item label="起息日" prop="valueDate">
            <el-date-picker v-model="financialInstruments.valueDate" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 80%;">李四</el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="债券名称" prop="debentureName">
        <el-col :span="11">
          <el-input v-model="financialInstruments.debentureName" placeholder="债券名称" style="width: 80%;"/>
        </el-col>
        <el-col :span="11">
          <el-form-item label="到息日" prop="dueDate">
            <el-date-picker v-model="financialInstruments.dueDate" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 80%;">李四</el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="市场" prop="market">
        <el-col :span="11">
          <el-input v-model="financialInstruments.market" placeholder="市场" style="width: 80%;"/>
        </el-col>
        <el-col :span="11">
          <el-form-item label="固定利率" prop="fixedInterestRate">
            <el-input v-model="financialInstruments.fixedInterestRate" placeholder="固定利率" style="width: 80%;"/>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="面值" prop="parValue">
        <el-col :span="11">
          <el-input v-model="financialInstruments.parValue" placeholder="面值" style="width: 80%;"/>
        </el-col>
      </el-form-item>
      <el-form-item class="de-button">
        <el-button type="primary" @click="onSubmit('financialInstruments')">保存</el-button>
        <el-button @click="onCancel()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import instrument from '@/api/instrument'
export default {
  data() {
    return {
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      financialInstruments: {
        debentureCode: '',
        valueDate: '',
        debentureName: '',
        dueDate: '',
        market: '',
        fixedInterestRate: '',
        parValue: ''
      },
      financialInstrumentsRules: {
        debentureCode: [{
          required: true,
          message: '请输入债券代码',
          trigger: 'blur'
        }],
        valueDate: [{
          required: true,
          message: '请输入起息日',
          trigger: 'blur'
        }],
        debentureName: [{
          required: true,
          message: '请输入债券名称',
          trigger: 'blur'
        }],
        dueDate: [{
          required: true,
          message: '请输入到息日',
          trigger: 'blur'
        }],
        market: [{
          required: true,
          message: '请输入市场',
          trigger: 'blur'
        }],
        fixedInterestRate: [{
          required: true,
          message: '请输入固定利率',
          trigger: 'blur'
        }],
        parValue: [{
          required: true,
          message: '请输入面值',
          trigger: 'blur'
        }],
      },
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid)=>{
        if(valid){
          instrument.save(this.financialInstruments).then(response => {
            this.$message({
              message: '恭喜你，保存成功',
              type: 'success'
            })
            Object.assign(this.$data.financialInstruments,this.$options.data().financialInstruments)
          }).catch(error => { // 请求失败
            this.$message({
              message: '添加失败',
              type: 'error'
            })
             Object.assign(this.$data.financialInstruments,this.$options.data().financialInstruments)
        })
        }
      })
    },
    onCancel() {
      this.$router.push({ path: '/table/table' })
    }
  }
}
</script>

<style scoped>
.right-form{
  width: 300px;
}
.left-form{
  width: 300px;
}
.app-container{
  color: white;
  margin-left: 120px;
}
.de-button{
  margin-top: 60px;
  margin-left: 300px;
}
</style>
